import "./Zone.ts";
import "./UI.ts";

world.onEntityContact(async ({ entity, other }) => {
  if (entity) {
    if (entity.id === "ToBeginning") {
      if (other.player) {
        other.player.dialog({
          type: GameDialogType.SELECT,
          title: `敬请期待`,
          content: `敬请期待！\n下一关\n序幕 The Beginning\n即将上线！`,
          options: ["了解"],
          titleBackgroundColor: new GameRGBAColor(0.54, 0.32, 0.18, 1), // 棕色标题背景
          titleTextColor: new GameRGBAColor(0.94, 0.91, 0.83, 1), // 米白色标题文字
          contentBackgroundColor: new GameRGBAColor(0.94, 0.91, 0.83, 1), // 米白色内容背景
          contentTextColor: new GameRGBAColor(0.54, 0.32, 0.18, 1), // 棕色内容文字
        });
      }
    } else if (entity.id === "ToPiano") {
      if (other.player) {
        other.player.dialog({
          type: GameDialogType.SELECT,
          title: `敬请期待`,
          content: `敬请期待！\n下一关\n钢琴 The Piano\n即将上线！`,
          options: ["了解"],
          titleBackgroundColor: new GameRGBAColor(0.54, 0.32, 0.18, 1), // 棕色标题背景
          titleTextColor: new GameRGBAColor(0.94, 0.91, 0.83, 1), // 米白色标题文字
          contentBackgroundColor: new GameRGBAColor(0.94, 0.91, 0.83, 1), // 米白色内容背景
          contentTextColor: new GameRGBAColor(0.54, 0.32, 0.18, 1), // 棕色内容文字
        });
      }
    }
  }
});

world.onPlayerJoin(async ({ entity }) => {
  if (
    entity.player.userId === "" ||
    entity.player.userId === "0" ||
    entity.player.userId === null
  ) {
    await entity.player.dialog({
      type: GameDialogType.SELECT,
      title: `抱歉！`,
      content: `抱歉！游客无法体验！`,
      options: ["退出地图"],
    });
    entity.player.kick();
  }
});

const spring = world.querySelector("#春天");
if (spring) {
  spring.enableInteract = true;
  spring.interactRadius = 6;
  spring.interactHint = `春天·The Spring`;

  spring.onInteract(async ({ entity }) => {
    const spring_ = await entity.player.dialog({
      type: GameDialogType.SELECT,
      title: spring.interactHint,
      content: `确定要游玩 ${spring.interactHint} 嘛？`,
      options: ["确定"],
    });
    if (spring_) {
      while (true) {
        try {
          let players = [entity];
          players = players.filter(
            (e) =>
              e.player.userId !== "" &&
              e.player.userId !== "0" &&
              e.player.userId !== null
          );
          await world.teleport("100459204", players);
          break;
        } catch (e: any) {
          console.log("传送失败：" + e.stack);
        }
        await sleep(1000);
      }
    }
  });
}

const beginning = world.querySelector("#序幕");
if (beginning) {
  beginning.enableInteract = true;
  beginning.interactRadius = 6;
  beginning.interactHint = `序幕·The Beginning`;

  beginning.onInteract(async ({ entity }) => {
    const beginning_ = await entity.player.dialog({
      type: GameDialogType.SELECT,
      title: beginning.interactHint,
      content: `确定要游玩 ${beginning.interactHint} 嘛？`,
      options: ["确定"],
    });
    if (beginning_) {
      while (true) {
        try {
          let players = [entity];
          players = players.filter(
            (e) =>
              e.player.userId !== "" &&
              e.player.userId !== "0" &&
              e.player.userId !== null
          );
          await world.teleport("100468647", players);
          break;
        } catch (e: any) {
          console.log("传送失败：" + e.stack);
        }
        await sleep(1000);
      }
    }
  });
}

const piano = world.querySelector("#钢琴");
if (piano) {
  piano.enableInteract = true;
  piano.interactRadius = 6;
  piano.interactHint = `钢琴·The Piano`;

  piano.onInteract(async ({ entity }) => {
    const piano_ = await entity.player.dialog({
      type: GameDialogType.SELECT,
      title: piano.interactHint,
      content: `确定要游玩 ${piano.interactHint} 嘛？`,
      options: ["确定"],
    });
    if (piano_) {
      while (true) {
        try {
          let players = [entity];
          players = players.filter(
            (e) =>
              e.player.userId !== "" &&
              e.player.userId !== "0" &&
              e.player.userId !== null
          );
          await world.teleport("100483575", players);
          break;
        } catch (e: any) {
          console.log("传送失败：" + e.stack);
        }
        await sleep(1000);
      }
    }
  });
}